<template>
  <div class="page">
    <el-form :inline="true">
      <el-form-item>
        <el-button type="primary" icon="el-icon-plus" @click="initForm"
          >新增</el-button
        >
      </el-form-item>
    </el-form>

    <el-table :data="list" border>
      <el-table-column label="ID" prop="id"></el-table-column>
      <el-table-column label="帮助中心名称" prop="title"></el-table-column>
      <el-table-column label="帮助中心分类" prop="helpId"></el-table-column>
      <el-table-column label="是否常见" prop="isCommon">
        <template slot-scope="scope">
          <el-switch
            :value="scope.row.isCommon"
            :active-value="1"
            :inactive-value="0"
          ></el-switch>
        </template>
      </el-table-column>

      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button type="primary" @click="edit(scope.row)">编辑</el-button>
          <el-button type="danger" @click="remove(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <div class="paganation">
      <el-pagination
        @current-change="handlePageChange"
        :current-page="queryParams.pageNum"
        :page-size="queryParams.pageSize"
        layout="total, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </div>

    <!-- 新增 -->
    <el-dialog title="帮助中心" :visible.sync="formDialogShow">
      <el-form :model="form" ref="ruleForm" :rules="rules">
        <el-form-item label="帮助中心名称" label-width="120px" prop="title">
          <el-input
            v-model="form.title"
            autocomplete="off"
            @input="change($event)"
          ></el-input>
        </el-form-item>
        <el-form-item label="帮助中心分类" label-width="120px" prop="helpId">
          <el-select v-model="form.helpId">
            <el-option
              v-for="item in cateList"
              :key="item.id"
              :label="item.catename"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="是否常见" label-width="120px" prop="isCommon">
          <el-switch
            v-model="form.isCommon"
            :active-value="1"
            :inactive-value="0"
          ></el-switch>
        </el-form-item>
        <el-form-item label="帮助中心内容" label-width="120px" prop="content">
          <Editor v-model="form.content"></Editor>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submit"> 确 定 </el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import {
  helpCateList,
  helpList,
  addHelp,
  editHelp,
  removeHelp,
} from "../../../api/ds/helpCenter";
import ImageUpload from "@/components/ImageUpload/index";
import Editor from "@/components/Editor";
export default {
  components: {
    ImageUpload,
    Editor,
  },
  props: {},
  data() {
    return {
      cateList: [],
      list: [],
      queryParams: {
        pageNum: 1,
        pageSize: 10,
      },
      total: 0,
      form: {
        helpId: "",
        title: "",
        content: "",
        isCommon: "",
        id: "",
      },
      rules: {
        catename: [
          { required: true, message: "请输入类目名称", trigger: "blur" },
        ],
      },

      formDialogShow: false,
    };
  },
  computed: {},
  watch: {},
  created() {
    this.getCateList();
    this.getData();
  },
  methods: {
    change(e) {
      this.$forceUpdate(e);
    },
    edit(row) {
      this.form.helpId = row.helpId;
      this.form.title = row.title;
      this.form.content = row.content;
      this.form.isCommon = row.isCommon;
      this.form.id = row.id;

      this.formDialogShow = true;
    },
    remove(row) {
      // console.log(row);
      const ids = row.id;
      this.$confirm('是否确认删除轮播图编号为"' + ids + '"的数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(function () {
          return removeHelp(ids);
        })
        .then(() => {
          this.getData();
          this.msgSuccess("删除成功");
        });
    },
    async getCateList() {
      let res = await helpCateList();
      if (res.code === 200) {
        this.cateList = res.rows;
      }
    },

    async getData() {
      let res = await helpList(this.queryParams);
      if (res.code === 200) {
        this.list = res.rows;
        this.total = res.total;
      }
    },

    handlePageChange(pageNum) {
      this.queryParams.pageNum = pageNum;
      this.getData();
    },

    initForm() {
      this.form = { helpId: "", title: "", content: "", isCommon: "", id: "" };
      this.formDialogShow = true;
    },

    async submit() {
      let res;
      this.$refs.ruleForm.validate(async (valid) => {
        if (valid) {
          console.log(this.form);
          if (this.form.id) {
            res = await editHelp(this.form);
          } else {
            res = await addHelp({
              helpId: this.form.helpId,
              title: this.form.title,
              content: this.form.content,
              isCommon: this.form.isCommon,
            });
          }

          if (res.code === 200) {
            this.formDialogShow = false;
            this.queryParams.pageNum = 1;
            this.getData();

            // 清理表单
            this.form = {
              helpId: "",
              title: "",
              content: "",
              isCommon: "",
              id: "",
            };
          }
        } else {
          return false;
        }
      });
    },
  },
  mounted() {},
  beforeCreate() {},
  beforeMount() {},
  beforeUpdate() {},
  updated() {},
  beforeDestroy() {},
  destroyed() {},
  activated() {},
  deactivated() {},
};
</script>
<style lang="scss" scoped>
.page {
  width: 100%;
  box-sizing: border-box;
  padding: 20px;

  .paganation {
    width: 100%;
    height: 50px;
    display: flex;
    flex-flow: row nowrap;
    justify-content: flex-start;
    align-items: center;

    box-sizing: border-box;
    padding-top: 0 25px;
  }
}
</style>
